package com.yidu.hbatsams.action;

import com.yidu.hbatsams.domain.vo.ModuleVo;
import com.yidu.hbatsams.domain.Role;
import com.yidu.hbatsams.domain.vo.RoleModuleVo;
import com.yidu.hbatsams.domain.vo.RoleVo;
import com.yidu.hbatsams.service.ModuleService;
import com.yidu.hbatsams.service.RoleService;
import com.yidu.hbatsams.utils.Node;
import com.yidu.hbatsams.utils.NodeRole;
import com.yidu.hbatsams.utils.State;
import com.yidu.hbatsams.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

/*
 * @Author 谭俊
 * @Description //角色
 * @Date 10:45 2019/10/27 0027
 * @Param
 * @return
 **/
@Controller
@RequestMapping("Role")
public class RoleContorller {


    @Resource
   private RoleService roleService;

    @Resource
    private ModuleService moduleService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询全部信息，不分页
     * @return
     */
    @RequestMapping("selectAll")
    @ResponseBody
    public List<Role> selectAll(Role role){


        List<Role> list = roleService.selectAll(role);


        return list;

    }



    /**
     * layui中table表分页，默认是page=1，limit=10，与pagehaper参数对应，
     * layui中还需要数据的总个数count
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @RequestMapping("findAll")
    @ResponseBody
    public Map<String,Object> findAll(@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize,String roleName) throws Exception {


        int count = roleService.count(roleName);


        Map<String,Object> map = new HashMap<>();

        List<Role> list = roleService.findAll(pageNum,pageSize,roleName);





        map.put("data",list);

        map.put("count",count);

        map.put("code", 0);

        map.put("msg", "");//消息

        return map;


    }



    /*
     * 查询所有模板，下拉树
     */
    @RequestMapping("findByAll")
    @ResponseBody
    public List<Node> findByAll(){
        List<ModuleVo> list =  moduleService.findAT();
        List<Node> listNode = new ArrayList<Node>();
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            ModuleVo moduleVo = (ModuleVo) iterator.next();
           // if(moduleVo.getModuleValidity().equals("有效")) {
                //新建一个node工具类对象
                Node node = new Node();
                //给工具类的id赋值
                node.setId(moduleVo.getModuleId());
                //给工具类的外键赋值
                node.setPid(moduleVo.getModModuleId());
                //给工具类的名称赋值
                node.setName(moduleVo.getModuleName());
                //最后将其加入到集合中
                listNode.add(node);
           // }
        }
        //新建一个node工具类对象
        Node node = new Node();
        //新建一个集合对象，将上一个集合加入到本集合中
        List<Node> nodeList2 = node.createTree(listNode);
        return nodeList2;

    }


    /*
     * 查询所有模板，树形组件
     */
    @RequestMapping("findByAllTwo")
    @ResponseBody
    public List<NodeRole> findByAllTwo(){
        List<ModuleVo> list = moduleService.findAT();
        List<NodeRole> listNode = new ArrayList<NodeRole>();
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            ModuleVo moduleVo = (ModuleVo) iterator.next();
                //新建一个node工具类对象
                NodeRole node = new NodeRole();
                //给工具类的id赋值
                node.setId(moduleVo.getModuleId());
                //给工具类的外键赋值
                node.setPid(moduleVo.getModModuleId());
                //给工具类的名称赋值
                node.setTitle(moduleVo.getModuleName());
                //最后将其加入到集合中
                listNode.add(node);
        }
        //新建一个node工具类对象
        NodeRole node = new NodeRole();
        //新建一个集合对象，将上一个集合加入到本集合中
        List<NodeRole> nodeList2 = node.createTree(listNode);
        return nodeList2;

    }


    /*
     * 查询所有模板，树形组件 ，是否选中
     */
    @RequestMapping("findByAllThree")
    @ResponseBody
    public List<NodeRole> findByAllThree(String id){
        List<ModuleVo> list = moduleService.findAT();
        Map<String,ModuleVo> map = new HashMap<>();
        for (int i = 0; i < list.size() ; i++) {
            map.put(list.get(i).getModModuleId(),list.get(i));
        }
        List<NodeRole> listNode = new ArrayList<NodeRole>();
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            ModuleVo moduleVo = (ModuleVo) iterator.next();
                //新建一个node工具类对象
                NodeRole node = new NodeRole();
                //给工具类的id赋值
                node.setId(moduleVo.getModuleId());
                //给工具类的外键赋值
                node.setPid(moduleVo.getModModuleId());
                //给工具类的名称赋值
                node.setTitle(moduleVo.getModuleName());
                ValueOperations<String,List<ModuleVo>> operations = redisTemplate.opsForValue();
                List<ModuleVo> lists = new ArrayList<>();
                if(redisTemplate.opsForList().size("moduleVo_"+id)==0){
                    lists = roleService.findRoleAllId(id);
                    if(lists.size()>0){
                        redisTemplate.opsForList().rightPushAll("moduleVo_"+id,lists);
                    }
                }else{
                    lists = redisTemplate.opsForList().range("moduleVo_"+id,0,-1);
                }
                for (int i = 0; i < lists.size(); i++) {
                    ModuleVo moduleVo1 = map.get(lists.get(i).getModuleId());
                    if(moduleVo1==null) {
                        if(lists.get(i).getModuleId().equals(moduleVo.getModuleId())) {
                            node.setChecked(true);
                            break ;
                        }else {
                            node.setChecked(false);
                        }
                    }
                }

                //最后将其加入到集合中
                listNode.add(node);
        }
        //新建一个node工具类对象
        NodeRole node = new NodeRole();
        //新建一个集合对象，将上一个集合加入到本集合中
        List<NodeRole> nodeList2 = node.createTree(listNode);
        return nodeList2;
    }


    /**
     * @param rolevo
     * @return
     */
    @RequestMapping("RoleInsert")
    @ResponseBody
    public State RoleInsert(RoleVo rolevo) {

        State bm=new State();
        rolevo.setRoleTime(Tools.getCurDateTime());

        if(rolevo.getRoleId()!=null && !"".equals(rolevo.getRoleId())) {

            roleService.update(rolevo);
        }else {

            roleService.insert(rolevo);
          //  roleService.add(rolevo);
        }
        bm.setState(1);
        bm.setMsg("操作成功");
        return bm;
    }


    @RequestMapping("RoleById")
    @ResponseBody
    public RoleVo RoleById(String id) {
        RoleVo rl=roleService.findById(id);
        return rl;
    }


    @RequestMapping("updateRolevalidity")
    @ResponseBody
    public State updateRolevalidity(String roleId,String roleValidity) {

        roleService.updateRolevalidity(roleId, roleValidity);

        State bm=new State();

        bm.setState(1);

        return bm;
    }


    /**
     * 删除数据
     * @param vo
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public State delete(RoleVo vo) {
        roleService.deleteUp(vo);
        State state = new State();
        state.setState(1);
        state.setMsg("操作成功");
        return state;
    }



}
